package rexsee.natives;

import java.io.File;

import rexsee.core.browser.Browser;
import rexsee.core.style.RexseeDrawable;
import rexsee.core.style.StyleSheet;
import rexsee.core.utilities.Utilities;
import rexsee.dialog.ImageMultiTouchListener;
import rexsee.dialog.ImageMultiTouchListener.OnImageDragedOverThresholdListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class GalleryAdapter extends BaseAdapter {

	private final Browser mBrowser;
	private final Drawable mIconBg;
	private final StyleSheet mStyle;
	private final String[] mDrawablePathes;
	private final ViewTag mParent;
	private final Gallery mGallery;

	private final View[] mViews;
	public GalleryAdapter(Browser browser, ViewTag parent, String[] options) {
		mBrowser = browser;
		mDrawablePathes = options;
		mViews = new View[options.length];
		mStyle = parent.style;
		mIconBg = RexseeDrawable.getDrawable(browser, parent.style.icon_background_color, null);
		mParent = parent;
		mGallery = (Gallery) mParent.view;
	}
	public GalleryAdapter(Browser browser, ViewTag parent, View[] options) {
		mBrowser = browser;
		mDrawablePathes = null;
		mIconBg = null;
		mStyle = null;
		mViews = new View[options.length];
		for (int i = 0; i < options.length; i++) {
			mViews[i] = options[i];
			Gallery.LayoutParams layoutParams;
			ViewTag tag = (ViewTag) mViews[i].getTag();
			if (tag != null) {
				layoutParams = new Gallery.LayoutParams(tag.style.getWidth(), tag.style.getHeight());
			} else {
				layoutParams = new Gallery.LayoutParams(Gallery.LayoutParams.FILL_PARENT, Gallery.LayoutParams.FILL_PARENT);
			}
			mViews[i].setLayoutParams(layoutParams);
		}
		mParent = parent;
		mGallery = (Gallery) mParent.view;
	}

	public void setTouchListener(ImageView imageView, int width, int height) {
		int threshold = Utilities.getInt(mStyle.distance_threshold, 0);
		if (threshold > 0 && mStyle.event_touch.equalsIgnoreCase("true")) {
			imageView.setClickable(true);
			if (mStyle.event_multitouch.equalsIgnoreCase("true")) {
				imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
				ImageMultiTouchListener multiTouchListener = new ImageMultiTouchListener(imageView, width, height, threshold, new ImageMultiTouchListener.OnImageDragedOverThresholdListener() {
					@Override
					public void dragedOverThreshold(int direction) {
						if (direction == OnImageDragedOverThresholdListener.DIRECTION_LEFT) {
							mGallery.onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, null);
							mGallery.onKeyUp(KeyEvent.KEYCODE_DPAD_LEFT, null);
						} else if (direction == OnImageDragedOverThresholdListener.DIRECTION_RIGHT) {
							mGallery.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
							mGallery.onKeyUp(KeyEvent.KEYCODE_DPAD_RIGHT, null);
						}
					}
				});
				imageView.setOnTouchListener(new ViewOnTouchListener(mBrowser, mParent.id, mStyle.event_touchmove.equalsIgnoreCase("true"), multiTouchListener));
			} else {
				imageView.setOnTouchListener(new ViewOnTouchListener(mBrowser, mParent.id, mStyle.event_touchmove.equalsIgnoreCase("true"), null));
			}
		}
	}
	private ImageView createImageView(String path) {
		try {
			ImageView imageView = new ImageView(mBrowser.getContext());
			if (mIconBg != null) imageView.setBackgroundDrawable(mIconBg);
			imageView.setPadding(mStyle.getIconPaddingLeft(), mStyle.getIconPaddingTop(), mStyle.getIconPaddingRight(), mStyle.getIconPaddingBottom());
			imageView.setScaleType(mStyle.getIconScaleType());

			int bounds = Utilities.getInt(mStyle.icon_bounds, -1);
			int imageWidth, imageHeight;

			if (bounds > 0) {
				File file = new File(Uri.parse(path).getSchemeSpecificPart());
				if (file != null && file.isFile() && file.canRead()) {
					BitmapFactory.Options options = new BitmapFactory.Options();
					options.inJustDecodeBounds = true;
					Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
					options.inJustDecodeBounds = false;
					options.inSampleSize = Math.max(1, Math.round(Math.max(options.outHeight / bounds, options.outWidth / bounds)));
					bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
					bitmap = (mStyle.effect.equalsIgnoreCase("reflectshader")) ? RexseeDrawable.createReflectShader(bitmap, 0.5f) : bitmap;
					imageView.setImageDrawable(new BitmapDrawable(bitmap));
					imageWidth = bitmap.getWidth();
					imageHeight = bitmap.getHeight();
				} else {
					imageWidth = 0;
					imageHeight = 0;
				}
			} else {
				Drawable drawable = RexseeDrawable.getDrawable(mBrowser, path, null);
				if (drawable != null) {
					drawable = (mStyle.effect.equalsIgnoreCase("reflectshader")) ? RexseeDrawable.createReflectShader(drawable, 0.5f) : drawable;
					imageView.setImageDrawable(drawable);
					imageWidth = drawable.getIntrinsicWidth();
					imageHeight = drawable.getIntrinsicHeight();
				} else {
					if (mStyle.icon != null) {
						imageView.setImageDrawable(mStyle.icon);
						imageWidth = mStyle.icon.getIntrinsicWidth();
						imageHeight = mStyle.icon.getIntrinsicHeight();
					} else {
						imageWidth = 0;
						imageHeight = 0;
					}
				}
			}

			int w = mStyle.getIconWidth();
			int h = mStyle.getIconHeight();
			if (w == Gallery.LayoutParams.WRAP_CONTENT) {
				w = imageWidth;
			} else if (w == Gallery.LayoutParams.FILL_PARENT) {
				w = mStyle.getWidth();
			}
			if (h == Gallery.LayoutParams.WRAP_CONTENT) {
				h = imageHeight;
			} else if (h == Gallery.LayoutParams.FILL_PARENT) {
				h = mStyle.getHeight();
			}

			Gallery.LayoutParams layoutParams = new Gallery.LayoutParams(w, h);
			imageView.setLayoutParams(layoutParams);

			setTouchListener(imageView, w, h);

			return imageView;

		} catch (Exception e) {
			mBrowser.exception(this.getClass().getSimpleName(), e);
			return null;
		}
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		return (mDrawablePathes != null) ? createImageView(mDrawablePathes[position]) : mViews[position];
	}
	@Override
	public int getCount() {
		return mViews.length;
	}
	@Override
	public Object getItem(int position) {
		return position;
	}
	@Override
	public long getItemId(int position) {
		return position;
	}

}
